﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLUNZ.Business;

namespace NLUNZ.Admin
{
    public partial class Default : System.Web.UI.Page
    {
        public CompanyInfo LatestNews { get; set; }
        public List<CompanyInfo> NewsList { get; set; }
        public List<Partner> Partners { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadNews();
            LoadPartner();
            LoadNoticeBoard();
            LoadCompanyBriefDescription();
            LoadAds();
        }

        private void LoadAds()
        {
            string imgUrl = Repository.LoadRandomImageUrl();
            if(!string.IsNullOrEmpty(imgUrl))
            {
                imgUrl = ResolveClientUrl("~/Images/Ads/" + imgUrl);
            }
            imgAds.ImageUrl = imgUrl; //ResolveClientUrl("~/Images/Ads/wallpaper-3.jpg");
            imgAds.Width = 320;
            imgAds.Height = 330;
        }


        private void LoadCompanyBriefDescription()
        {
            var key = "AboutUsBrief";
            var brief = Repository.GetValueFromDictionary(key);
            literalAboutUs.Text = brief ?? string.Empty;

        }

        private void LoadNoticeBoard()
        {
            List<String> nb = Repository.GetLatestNoticeBoard();
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul id='NoticeBoardScroller'>");
            foreach (string noticeBoard in nb)
            {
                sb.AppendFormat("<li>{0}</li>", noticeBoard);
            }
            sb.Append("</ul>");
            scroller.Text = sb.ToString();
        }

        private void LoadPartner()
        {
            Partners = Repository.GetAllPartners();
        }

        private void LoadNews()
        {
            List<CompanyInfo> latestNews = Repository.GetLatestNews(6);
            if (latestNews.Count > 0)
            {
                //get the top 1 news
                LatestNews = latestNews[0];

                if (latestNews.Count > 1)
                {
                    NewsList = latestNews.Skip(1).ToList();
                }
            }
        }

        protected void RadTextBoxTracking_TextChanged(object sender, EventArgs e)
        {
            SearchTracking();
        }

        protected void Signout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("~/Admin/Login.aspx");
        }

        protected void btnEditPartners_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/EditPartners.aspx");
        }

        protected void btnEditNews_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/EditNews.aspx");
        }

        protected void btnEditTracking_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/TrackingEdit.aspx");
        }

        protected void btnEditMembership_Click(object sender, EventArgs e)
        {

            Response.Redirect("~/Admin/EditUsers.aspx");
        }

        private void SearchTracking()
        {
            string keys = RadTextBoxTracking.Text;
            Response.Redirect("~/Tracking.aspx?keys=" + keys);
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            SearchTracking();
        }

        protected void btnNoticeBoard_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/NoticeBoardEdit.aspx");
        }

        protected void btnAboutUs_Click(object sender, EventArgs e)
        {

            Response.Redirect("~/Admin/AboutUsBriefEdit.aspx");
        }

        protected void btnLatestAds_Click(object sender, EventArgs e)
        {

            Response.Redirect("~/Admin/LatestAdsSlidesEdit.aspx");
        }

    }
}